home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / os2 / pmnos11s / at.c < prev    next >
C/C++ Source or Header  |  1993-07-30  |  6KB  |  251 lines

  1. /*    Timed execution routine. Starts a timer and executes a sequence
  2.  *    of commands when expired.
  3.  *
  4.  *    Added by IW0CNB - Feb 1992
  5.  *  'at mm' format added by WG7J - 920805
  6.  */
  7.  
  8. #include <time.h>
  9. #if !defined(OS2)
  10. #include <dos.h>
  11. #endif
  12. #include "global.h"
  13. #include "timer.h"
  14. #include "cmdparse.h"
  15.  
  16. void atcmd __ARGS((char *command));
  17.  
  18. /* List of events; We keep note of all timer processes generated by the 
  19.  * at command.
  20.  */
  21. struct at_list {
  22.     struct at_list *next;    /* Linked-list pointer */
  23.     struct timer *at_timer;
  24. };
  25.  
  26. #define    NULLATLIST    (struct at_list *)0
  27.  
  28. static struct at_list *Head_loe = NULLATLIST;    /* Head of List Of Events */
  29.  
  30. int
  31. doat(argc,argv,p)
  32. int argc;
  33. char *argv[];
  34. void *p;
  35. {
  36. #if !defined(OS2)
  37.     struct tm *exp;
  38.     struct timer *t;
  39.     char *cp;
  40.     time_t nowtime;
  41.     unsigned long time1;
  42.     struct tm tm;
  43.     extern struct timer *Timers;
  44.     struct at_list *loe;    /* List of events */
  45.     char *Errmsg = "Usage:\nat yymmddhhmm <cmd>\nat hhmm <cmd>\nat mm <cmd>\nat now+hhmm <cmd>\n";
  46.  
  47.     if(argc < 2){        /* Print list of pending at commands */
  48.         tputs("List of events:\n");
  49.         for(t = Timers;t != NULLTIMER;t = t->next){
  50.             if(t->func == (void (*)())atcmd){
  51.             time(&nowtime);
  52.             nowtime = (time_t)(read_timer(t) / 1000L + (unsigned long)nowtime);
  53.             cp = ctime(&nowtime);
  54.             rip(cp);
  55.             tprintf("At: %s - Command: %s\n",cp,t->arg);
  56.            }
  57.         }
  58.         return 0;
  59.     }
  60.  
  61.     if(argc < 3){
  62.         tputs(Errmsg);
  63.         return 0;
  64.     }
  65.  
  66.     exp = (struct tm *)mallocw(sizeof(struct tm));
  67.  
  68.     cp=mallocw(5);
  69.  
  70.     switch(strlen(argv[1])){
  71.        case 10:    /* Full date and time given */
  72.         cp[0]=argv[1][0];
  73.         cp[1]=argv[1][1];
  74.         cp[2]='\0';
  75.     
  76.         exp->tm_year = 1900 + atoi(cp);
  77.         if(exp->tm_year > 1999) goto error;
  78.  
  79.         cp[0]=argv[1][2];
  80.         cp[1]=argv[1][3];
  81.         cp[2]='\0';
  82.  
  83.         exp->tm_mon = (char)atoi(cp);
  84.         if(exp->tm_mon > 12) goto error;
  85.  
  86.         cp[0]=argv[1][4];
  87.         cp[1]=argv[1][5];
  88.         cp[2]='\0';
  89.  
  90.         exp->tm_mday = (char)atoi(cp);
  91.         if(exp->tm_mday > 31) goto error;
  92.  
  93.         cp[0]=argv[1][6];
  94.         cp[1]=argv[1][7];
  95.         cp[2]='\0';
  96.  
  97.         exp->tm_hour = (char)atoi(cp);
  98.         if(exp->tm_hour > 23) goto error;
  99.  
  100.         cp[0]=argv[1][8];
  101.         cp[1]=argv[1][9];
  102.         cp[2]='\0';
  103.  
  104.         exp->tm_min = (char)atoi(cp);
  105.         if(exp->tm_min > 59) goto error;
  106.  
  107.         exp->tm_sec = 0;
  108.  
  109.         time(&nowtime);
  110.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  111.         if(time1 < (unsigned long)nowtime) goto error;
  112.  
  113.         break;
  114.  
  115.        case 4:  /* Only time given, so apply current date */
  116.         getdate(exp_date);
  117.         cp[0]=argv[1][0];
  118.         cp[1]=argv[1][1];
  119.         cp[2]='\0';
  120.  
  121.         exp->tm_hour = (char)atoi(cp);
  122.         if(exp->tm_hour > 23) goto error;
  123.  
  124.         cp[0]=argv[1][2];
  125.         cp[1]=argv[1][3];
  126.         cp[2]='\0';
  127.  
  128.         exp->tm_min = (char)atoi(cp);
  129.         if(exp->tm_min > 59) goto error;
  130.  
  131.         exp->tm_sec = 0;
  132.  
  133.         time(&nowtime);
  134.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  135.         if(time1 < (unsigned long)nowtime){    /* Requested time has passed */
  136.             time1 += 86400L;        /* So book him for tomorrow */
  137.         }
  138.         break;
  139.  
  140.  
  141.        case 2:  /* Only minutes given, so apply current time & date - WG7J */
  142.         tm.tm_min = (char)atoi(argv[1]);
  143.         if(tm.tm_min > 59) goto error;
  144.  
  145.         /* get today's date */
  146.         getdate(exp_date);
  147.         tm.tm_year = exp_date->da_year - 1900;
  148.         tm.tm_mday = exp_date->da_day;
  149.         tm.tm_mon = exp_date->da_mon - 1;
  150.  
  151.         /* get current time */
  152.         gettime(exp_time);
  153.         tm.tm_hour = exp_time->ti_hour;
  154.         /* if we're already past the minute, do it next hour ! */
  155.         if(exp_time->ti_min > tm.tm_min)
  156.             tm.tm_hour++;
  157.  
  158.         /* now adjust this for day boundaries, etc. */
  159.         tm.tm_sec = 0;
  160.         tm.tm_isdst = 0;
  161.         time1 = mktime(&tm);
  162.         time(&nowtime);
  163.         break;
  164.  
  165.        case 8:    /* now+hhmm given */
  166.         strncpy(cp,argv[1],4);
  167.         cp[4]='\0';
  168.         if(strcmp(cp,"now+") != 0) goto error;
  169.  
  170.         cp[0]=argv[1][4];
  171.         cp[1]=argv[1][5];
  172.         cp[2]='\0';
  173.  
  174.         time1=(unsigned long)atoi(cp)*3600L;
  175.  
  176.         cp[0]=argv[1][6];
  177.         cp[1]=argv[1][7];
  178.         cp[2]='\0';
  179.  
  180.         time1+=(unsigned long)atoi(cp)*60L;
  181.         time(&nowtime);
  182.         time1+=(unsigned long)nowtime;
  183.         break;
  184.  
  185.        default:
  186. error:        tprintf(Errmsg);
  187.         free(exp_date);
  188.         free(exp_time);
  189.         free(cp);
  190.         return 0;
  191.  
  192.     } /* switch */
  193.  
  194.     free(cp);
  195.     free(exp_time);
  196.     free(exp_date);
  197.  
  198.     t=(struct timer *)mallocw(sizeof(struct timer));
  199.  
  200.     set_timer(t,(time1 - (unsigned long)nowtime) * 1000L);
  201.     t->state=TIMER_RUN;
  202.     t->func=(void (*)())atcmd;
  203.     t->arg=(char *)mallocw(strlen(argv[2])+2);
  204.     strcpy(t->arg,argv[2]);
  205.     start_timer(t);
  206.  
  207.     /* Add the new timer to the head of List Of Events */
  208.     loe=(struct at_list *)mallocw(sizeof(struct at_list));
  209.     loe->at_timer=t;
  210.     loe->next=Head_loe;
  211.     Head_loe=loe;
  212. #endif
  213.     return 0;
  214. }
  215.  
  216. /* Function to be called on timer expiration to execute a command */
  217. void
  218. atcmd(command)
  219. char *command;
  220. {
  221.     extern struct cmds Cmds[];
  222.     struct at_list *loe, *p;
  223.  
  224.     log(-1,"AT command: %s",command);
  225.  
  226.     /* Free up memory for expired at commands */
  227.     p=Head_loe;
  228.     loe=Head_loe;
  229.     while(loe != NULLATLIST){
  230.         if(loe->at_timer->state == TIMER_EXPIRE){
  231.             free(loe->at_timer);    /* Free timer */
  232.             if(loe == Head_loe){
  233.                 Head_loe=loe->next;
  234.                 p=loe->next;
  235.                 free(loe);
  236.                 loe=p;
  237.                 p=Head_loe;
  238.             } else {
  239.                 p->next=loe->next;
  240.                 free(loe);
  241.                 loe=p->next;
  242.             }
  243.         } else {    /* Not expired, go on */
  244.             if(loe != Head_loe) p=p->next;
  245.             loe=loe->next;
  246.         }
  247.     }
  248.     cmdparse(Cmds,command,NULL);    /* Go with requested command */
  249.     free(command);
  250. }
  251.